<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-06 16:23:31
 * @LastEditTime: 2019-09-16 09:01:00
 * @LastEditors: Please set LastEditors
 -->
<script>
export default {
  created () {
    // 调用API从本地缓存中获取数据
    /*
     * 平台 api 差异的处理方式:  api 方法统一挂载到 mpvue 名称空间, 平台判断通过 mpvuePlatform 特征字符串
     * 微信：mpvue === wx, mpvuePlatform === 'wx'
     * 头条：mpvue === tt, mpvuePlatform === 'tt'
     * 百度：mpvue === swan, mpvuePlatform === 'swan'
     * 支付宝(蚂蚁)：mpvue === my, mpvuePlatform === 'my'
     */

    let logs
    if (mpvuePlatform === 'my') {
      logs = mpvue.getStorageSync({key: 'logs'}).data || []
      logs.unshift(Date.now())
      mpvue.setStorageSync({
        key: 'logs',
        data: logs
      })
    } else {
      logs = mpvue.getStorageSync('logs') || []
      logs.unshift(Date.now())
      mpvue.setStorageSync('logs', logs)
    }

    // 获取设备信息
    wx.getSystemInfo({
      success(res){
        const equi = res.brand + '  -  ' + res.model + '  -  ' +res.system
        console.log(equi)
        wx.setStorageSync('equi',equi)
      },
      fail(res){
        console.log(res)
      }
    })

    // 自动更新
    if (wx.canIUse("getUpdateManager")) {
      console.log('canIUse is ok')
      let updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate((res) => {
        // 请求完新版本信息的回调
        console.log('请求完新版本信息的回调')
        console.log(res);
        if(res.hasUpdate){
          updateManager.onUpdateReady(() => {
            console.log('更新提示')
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: (res) => {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                } else if (res.cancel) {
                  return false;
                }
              }
            })
          })
          updateManager.onUpdateFailed(() => {
            console.log('新的版本下载失败')
            wx.hideLoading();
            wx.showModal({
              title: '升级失败',
              content: '新版本下载失败，请检查网络！',
              showCancel: false
            });
          });
        }else{
          console.log('已是最新版本')
        }
      })
    }

  },
  log () {
    console.log(`log at:${Date.now()}`)
  }


  
}
</script>

<style>
/* page{
  padding-top:100rpx;
} */
/* .container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
} */
/* this rule will be remove */
* {
  transition: width 2s;
  -moz-transition: width 2s;
  -webkit-transition: width 2s;
  -o-transition: width 2s;
}
</style>
